package com.lm.leetcode.dualpointer;

/**
 * @Auther: Min.Liu
 * @Description:
 * 给定一个二进制数组 nums ， 计算其中最大连续 1 的个数。
 *
 * 示例 1：
 * 输入：nums = [1,1,0,1,1,1]
 * 输出：3
 * 解释：开头的两位和最后的三位都是连续 1 ，所以最大连续 1 的个数是 3.
 * 示例 2:
 * 输入：nums = [1,0,1,1,0,1]
 * 输出：2
 * @Date: Created in 9:14 2025/5/12
 */
public class findMaxConsecutiveOnesDemo {
    public static void main(String[] args) {
        findMaxConsecutiveOnesDemo demo = new findMaxConsecutiveOnesDemo();
        int[] nums= {1,1,0,1,1,1};
        //int[] nums = {1, 0, 1, 1, 0, 1};
        System.out.println(demo.findMaxConsecutiveOnes2(nums));
    }

    //强行使用双指针
    public int findMaxConsecutiveOnes(int[] nums) {
        int fast = 0;
        int slow = 0;
        int max = 0;
        while (fast < nums.length) {
            if (nums[fast] == 1) {
                max = fast - slow + 1 > max ? fast - slow + 1 : max;
                fast++;
            } else {
                if (fast > slow) {
                    slow++;
                } else {
                    fast++;
                    slow++;
                }
            }
        }
        return max;
    }

    //非双指针  贪婪获取
    public int findMaxConsecutiveOnes2(int[] nums) {
        int max = 0;
        int temp = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 1) {
                temp++;
            } else {
                temp = 0;
            }
            max = temp > max ? temp : max;
        }
        return max;
    }
}
